//
// file : win_test.cpp
// date : 09/17/07
//
// 
#include <stdio.h>
#include <string.h>

#include "inet_ipc.h"
#include "inet_apc.h"
#include <mswsock.h>

// udp ping

//
// a timer routine
//
void __stdcall ping_(struct utimer_t *tb)
{
    SYSTEMTIME st;
    char      str[200];
    ipccb_t  *ipc = (ipccb_t*)(tb->ut_param[0]);
    int       len;

    GetSystemTime(&st);

    len = sprintf(str, "Ping to(%s:%d) %.2d%.2d%.2d:%.2d%.2d%.2d.%.3d\n",
             inet_ntoa(ipc->toaddr.sin_addr), ntohs(ipc->toaddr.sin_port),
             st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);

    len = ipc_writing(ipc->sessid, str, len);

    ipc_print("[%d]%s", len, str);
}

//
// a reader
//
long __stdcall pong_(void *ctx, long len)
{
    ipccb_t  *ipc = (ipccb_t*)ctx;

    // incoming data in ibound
    ipc->ibound[1]='o';

    len += ipc->ipoint;

    ipc->ibound[len] = '\0';

    len = sendto( ipc->socket, ipc->ibound, len, 0, 
                  (struct sockaddr*)&ipc->toaddr, sizeof(ipc->toaddr) );

    ipc_print("[%d]%s", len, ipc->ibound);

    return 0;
}

//
// a reader
//
long __stdcall echo_(void *ctx, long len)
{
    ipccb_t  *ipc = (ipccb_t*)ctx;

    // incoming data in ibound
    ipc->ibound[1]='o';

    len += ipc->ipoint;

    ipc->ibound[len] = '\0';

    ipc_print("echo %s", ipc->ibound);

    return 0;
}

long udp_ping(char *addr_str, long didx)
{
    long sid;
    long e;

    sid = udp_session(didx);

    if( sid < 0 )
    {
        printf(" udp_ping: udp_session=%d\n", sid);
        return sid; // error
    }
    // set reader
    e = set_freader(sid, echo_);

    //e = udp_connect(sid, addr_str);
    e = set_to_addr(sid, addr_str);

    if( e < 0 )
    {
        printf(" udp_ping: udp_connect(%s)=%d\n", addr_str, e);
        return e;
    }
    // set ping timer
    utimer_t  ut;

    memset(&ut, 0, sizeof(ut));

    ut.ut_cycle = 1000;
    ut.ut_delay = 1500;
    ut.ut_awake = ping_;
    ut.ut_param[0] = ipc_context(sid);

    ipc_loadtmr(sid, &ut);

    return sid;
}

long udp_pong(char *addr_str, long didx)
{
    long sid;
    long e;

    sid = udp_session(didx);

    if( sid < 0 )
    {
        printf(" udp_pong: udp_session=%d\n", sid);
        return sid; // error
    }

    // set reader
    e = set_freader(sid, pong_);

    e = udp_binding(sid, addr_str);

    if( e < 0 )
    {
        printf(" udp_ping: udp_binding(%s)=%d\n", addr_str, e);
        return e;
    }

    return sid;
}

#include <signal.h>

static g_quit = 0;

void sig_int(int sig)
{
    if( sig == SIGINT ) g_quit = 1;
}


int main(int argc, char *argv[])
{
    unsigned short port;
    unsigned long  addr;
    long didx=0;
    long sid;

    port = 0;
    addr = 0;

    if( argc < 3 )
    {
print_help:
        printf("Uasge:\t%s {ping|pong} [ip addr:]port\n", argv[0]);
        return 0;
    }

    signal(SIGINT, sig_int);

    if( stricmp( argv[1], "ping") == 0 )
    {
        didx = ipc_startup(5, 0);

        sid = udp_ping( argv[2], didx );
    }
    else if( stricmp( argv[1], "pong") == 0 )
    {
        didx = ipc_startup(5, 0);

        sid = udp_pong( argv[2], didx );
    }
    else goto print_help;

    while( sid > 0 && g_quit == 0 ) Sleep(1000);

    if( didx )
    {
        if( sid > 0 ) ipc_shutoff( sid );
    }

    ipc_cleanup( didx);

    return 0;
}

 //udp ping 
